<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <ChangeCatalog/>
            </n-icon>
            <Handle id="KeyWordsExtraction-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="KeyWordsExtraction-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>文本清洗</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="文本清洗">
                <n-select
                    v-model:value="symbol"
                    multiple
                    :options="symbolEnum"
                    value-field="label"
                    @update:value="popup.reopen"
                    placeholder="选择所需的清除符号"
                />
                <n-checkbox v-model:checked="deduplication">
                    文本去重（找出多行文本中有重复的那些行，最终得到没有重复行的文本数据）
                </n-checkbox>
                <n-divider />
                <n-input type="textarea" v-model:value="props.data.text"/>
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="implementation">清洗</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {ChangeCatalog} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()


// 操作
const deduplication = ref(false)
const symbol = ref([])
const symbolEnum = [
    {label:"换行"},
    {label:"空行"},
    {label:"空格"},
    {label:"特殊字符"},
    {label:"html"},
    {label:"停用词"},
]
const text = ref("")
const implementation = () => {
    workApi.post("perform_tasks/text_cleaning", {
        data: props.data.text,
        symbol: symbol.value,
        deduplication: deduplication.value,
    }).then(r => {
        console.log(r)
        props.data.text = r.data
    })
}
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    /**
     * 点击外部
     */
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})


const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.text = j.data.text
                    console.log(props.data)
                    return
                }
            }
        }
    }
}
// 单页数据量
const pagination = {
    pageSize: 10
}

</script>

<style scoped lang="less">
header {
    display: flex;
    justify-content: space-between;

    p {
        display: flex;

        span {
            display: flex;
            align-items: center
        }

        .n-input-number {
            width: 100px;
        }
    }
}


.dict {
    > div {
        width: 500px;
    }
}
</style>